﻿using UnityEngine;
using System.Collections;
using PathologicalGames;

public class BattleEffectObj 
{
    private ParticleSystem[] m_pPrticles;
    private NcEffectAniBehaviour[] m_pNcEffs;

    public int m_iId;
    public HeroEff m_pConfig;
    public GameObject m_pGameobj;
    public GameObject m_pRender;

    public void OnCreate(int id, HeroEff config) 
    {
        m_iId = id;
        m_pConfig = config;
        m_pGameobj = new GameObject();
        m_pGameobj.name = "Effect_" + config.id + "_" + m_iId;
        m_pRender = BattleEffectMgr.Instance.GetCloneEffRes(m_pConfig.path);
        m_pRender.transform.SetParent(m_pGameobj.transform);
        m_pRender.transform.localPosition = Vector3.zero;
        m_pRender.transform.localEulerAngles = Vector3.zero;

        m_pPrticles = m_pRender.GetComponentsInChildren<ParticleSystem>(true);
        m_pNcEffs = m_pRender.GetComponentsInChildren<NcEffectAniBehaviour>(true);

        //自动删除
        if (config.lifeTime !=0)
        {
            BattleEffectMgr.Instance.AddTimer(delegate ()
            {
                BattleEffectMgr.Instance.DelEffectObj(m_iId);
            }, config.lifeTime, "特效OBJ：" + config.id);
        }
    }

    public void OnUpdate(float deltaTime) 
    {
        for (int i = 0; i < m_pPrticles.Length; i++)
        {
            m_pPrticles[i].Simulate(deltaTime,true,false);
        }
        for (int i = 0; i < m_pNcEffs.Length; i++)
        {
            m_pNcEffs[i].SetTimeScale(BattleEffectMgr.Instance.GetTimeSpeed());
        }
    }

    public void OnDelete() 
    {
        string prefabName = SlgFightUtil.SubPath2ObjName(m_pConfig.path);
        if (PoolManager.Pools[SlgFightDef.c_strEffPoolName].IsContainsPrefab(prefabName))
        {
            //Debug.Log("回收特效");
            PoolManager.Pools[SlgFightDef.c_strEffPoolName].Despawn(m_pRender.transform,
                PoolManager.Pools[SlgFightDef.c_strEffPoolName].group);
        }
        GameObject.DestroyObject(m_pGameobj);
    }
}
